package com.chinamsp.configuration;

import static com.google.common.collect.Lists.newArrayList;

import java.util.List;

import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.OperationBuilderPlugin;
import springfox.documentation.spi.service.contexts.OperationContext;

/**
 * 扩展 swagger operation 以便于实现  全局配置添加
 * 
 * @author shifenghu
 *
 */
@Component
@Order(Ordered.HIGHEST_PRECEDENCE + 100)
public class ExtendedOperationParameterBuilder implements OperationBuilderPlugin {
	@Override
	public void apply(OperationContext context) {
		// 获取 忽略 region 的请求 注解
//		Optional<HFAPIOperation> region = context.findAnnotation(HFAPIOperation.class);
//		// 如果方法不存在此注解，则添加 region 和 token字段
		//addParameters(context);
		
	}
	
	/**
	 * 将 region 和token 添加到 context中
	 * @param context
	 */
	protected void addParameters(OperationContext context){
		List<Parameter> parameters = newArrayList(
    			new ParameterBuilder()
    			.name("X-Auth-Token")
    			.description("用户当前登录的token")
    			.modelRef(new ModelRef("string"))
    			.parameterType("header")
    			.required(false)
    			.build()
		);
		context.operationBuilder().parameters(parameters);
	}
	
	@Override
	public boolean supports(DocumentationType delimiter) {
		return true;
	}
}
